#ifndef YINMEI_H
#define YINMEI_H

#include <string>
#include <memory>
#include <functional>

namespace yinmei {

    enum class PrintStatus {
        IDLE,
        CONVERTING,    
        PRINTING,      
        PRINT_ENDING,   
        COMPLETED,      
        ERROR           
    };

    enum class ConvertResult {
        SUCCESS,        
        FILE_NOT_FOUND, 
        CONVERT_FAILED, 
        INVALID_FORMAT 
    };

    struct PrinterConfig {
        int resolution = 300;
        int unit = 1200; 
        int grey = 1;  
        int duplex = 120; 
        int revert = 0; 
        int angle = -90;
        
        std::string toOptionsString() const;
    };

    class YinmeiPrinter {
    public:
        YinmeiPrinter();
        ~YinmeiPrinter();

        bool setInputFile(const std::string& filePath);
        
        std::string getInputFile() const;

        ConvertResult convertImage();
        ConvertResult convertImage(const std::string& outputPath);
        
        std::string getConvertedFilePath() const;
        bool startPrint();
        bool startPrint(const std::string& binFilePath);
        PrintStatus queryPrintStatus();
        
        bool isPrintCompleted();

        void setPrinterConfig(const PrinterConfig& config);
        PrinterConfig getPrinterConfig() const;

        void setWorkingDirectory(const std::string& workDir);
        std::string getWorkingDirectory() const;

        std::string getLastError() const;

        static std::string getAbsolutePath(const std::string& path);
        static bool fileExists(const std::string& path);

    private:
        std::string m_inputFile; 
        std::string m_convertedFile;  
        std::string m_workingDir;     
        std::string m_lastError;      
        
        PrinterConfig m_config; 
        PrintStatus m_currentStatus;
        
        time_t m_lastStatusTime;
        int m_completedStatusCount;

        bool executeCommand(const std::string& command, std::string& output);
        std::string generateOutputFileName(const std::string& inputFile);
        PrintStatus parseStatusOutput(const std::string& output);
        bool isValidImageFile(const std::string& filePath);
        void updateStatus(PrintStatus status);
        void resetStatusCounter();
    };

}

#endif // YINMEI_H
